package com.comm;

import com.dao.IMyip;
import com.model.MyIp;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Order(3)
@Component
@Aspect
public class IPAopAspect {
    @Autowired
    private IMyip iMyip;


    @Around("@annotation(com.comm.IPAnno)")
    public Object aroundAd(ProceedingJoinPoint pjp){
        System.out.println("进入环形通知方法");

        MethodSignature methodSignature=(MethodSignature) pjp.getSignature();

        Method method=methodSignature.getMethod();

        IPAnno ipAnno=method.getAnnotation(IPAnno.class);
        String oper=ipAnno.OperateType();

        MyIp myIp=new MyIp();
        myIp.setUserid(44);
        myIp.setOpname("接口描述："+oper);
        myIp.setIp(HttpContextUtil.getIPAddress());

        Object result=null;
        try {

            try {
                result =pjp.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

            myIp.setOpname("正常");

        }catch (Exception throwable){
            myIp.setOpname("失败");
        }finally {
            iMyip.addMyIp(myIp);
            System.out.println("存入");
        }
        System.out.println("结束环形通知方法");
        return result;
    }
}
